package com.ai.service.made.impl;

import com.ai.common.CommonResponse;
import com.ai.entity.made.MaintenancePersonnelInformation;
import com.ai.entity.made.page.MaintenancePersonnelInformationPage;
import com.ai.mapper.made.MaintenancePersonnelInformationMapper;
import com.ai.model.dto.MaintenancePersonnelInformationDto;
import com.ai.model.excelParam.MaintenancePersonnelInformationExcelDto;
import com.ai.service.made.IMaintenancePersonnelInformationService;
import com.ai.util.SecurityUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2025-01-08
 */
@Service
public class MaintenancePersonnelInformationServiceImpl extends ServiceImpl<MaintenancePersonnelInformationMapper, MaintenancePersonnelInformation> implements IMaintenancePersonnelInformationService {

    @Override
    public CommonResponse addMaintenancePersonnelInformation(MaintenancePersonnelInformationDto maintenancePersonnelInformationDto) {

        MaintenancePersonnelInformation maintenancePersonnelInformation = new MaintenancePersonnelInformation();
        BeanUtils.copyProperties(maintenancePersonnelInformationDto, maintenancePersonnelInformation);
        maintenancePersonnelInformation.setRegionBaseId(SecurityUtils.getLoginRegionBaseId());
        maintenancePersonnelInformation.setCreateTime(LocalDateTime.now());
        maintenancePersonnelInformation.setCreateUser(SecurityUtils.getLoginUserName());
        maintenancePersonnelInformation.setUpdateTime(LocalDateTime.now());
        maintenancePersonnelInformation.setUpdateUser(SecurityUtils.getLoginUserName());

        this.save(maintenancePersonnelInformation);

        return CommonResponse.ok("添加成功");
    }

    @Override
    public CommonResponse deleteMaintenancePersonnelInformation(MaintenancePersonnelInformationDto maintenancePersonnelInformationDto) {

        boolean b = this.removeById(maintenancePersonnelInformationDto.getId());

        return b?CommonResponse.ok("删除成功"):CommonResponse.error("删除失败");

    }

    @Override
    public CommonResponse modifyInfoMaintenancePersonnelInformation(MaintenancePersonnelInformationDto maintenancePersonnelInformationDto) {

        MaintenancePersonnelInformation maintenancePersonnelInformation = this.getById(maintenancePersonnelInformationDto.getId());
        if (maintenancePersonnelInformation != null) {
            BeanUtils.copyProperties(maintenancePersonnelInformationDto, maintenancePersonnelInformation);
            maintenancePersonnelInformation.setUpdateTime(LocalDateTime.now());
            maintenancePersonnelInformation.setUpdateUser(SecurityUtils.getLoginUserName());
            this.updateById(maintenancePersonnelInformation);
            return CommonResponse.ok("修改成功");
        }

        return CommonResponse.error("当前检修人员信息不存在");
    }

    @Override
    public IPage selectByPage(MaintenancePersonnelInformationPage maintenancePersonnelInformationPage) {
        QueryWrapper<MaintenancePersonnelInformation> queryWrapper = maintenancePersonnelInformationPage.getQuery(new QueryWrapper<MaintenancePersonnelInformation>());

        MaintenancePersonnelInformationPage informationPage = this.page(maintenancePersonnelInformationPage, queryWrapper);

        return informationPage;
    }


    @SneakyThrows
    @Override
    public CommonResponse importMaintenancePersonExcel(MultipartFile multipartFile)  {

        LinkedList<MaintenancePersonnelInformation> informationLinkedList = new LinkedList<>();
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        EasyExcel.read(multipartFile.getInputStream(), MaintenancePersonnelInformationExcelDto.class,new PageReadListener<MaintenancePersonnelInformationExcelDto>(dataList -> {
            dataList.forEach(maintenancePersonnelInformationExcelDto -> {
                MaintenancePersonnelInformation maintenancePersonnelInformation = new MaintenancePersonnelInformation();
                maintenancePersonnelInformation.setRegionBaseId(SecurityUtils.getLoginRegionBaseId());
                BeanUtils.copyProperties(maintenancePersonnelInformationExcelDto, maintenancePersonnelInformation);
                maintenancePersonnelInformation.setCreateTime(LocalDateTime.now());
                maintenancePersonnelInformation.setCreateUser(SecurityUtils.getLoginUserName());
                maintenancePersonnelInformation.setUpdateUser(SecurityUtils.getLoginUserName());
                maintenancePersonnelInformation.setUpdateTime(LocalDateTime.now());
                informationLinkedList.add(maintenancePersonnelInformation);
            });
        })).sheet().doRead();;

        boolean saveBatch = this.saveBatch(informationLinkedList);

        return saveBatch? CommonResponse.ok("导入成功"):CommonResponse.error("导入失败");
    }


}
